$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/sage/exam/score/scoreList',
        datatype: "json",
        colModel: [			
            { label: '学生', name: 'studentName', index: 'studentName', width: 80 },
            { label: '过关分数', name: 'examPassScore', index: 'examPassScore', width: 80 }, 			
            { label: '试卷名称', name: 'examName', index: 'examName', width: 80 }, 			
			{ label: 'scoreId', name: 'scoreId', index: 'scoreId', width: 50, key: true , hidden:true},
			{ label: 'examtionId', name: 'examtionId', index: 'examtionId', width: 50, hidden:true},
			{ label: 'studentId', name: 'studentId', index: 'studentId', width: 50, hidden:true},
			{ label: '考试得分', name: 'examScore', index: 'examScore', width: 80 }, 			
			{ label: '交卷时间', name: 'subExamTime', index: 'subExamTime', width: 80 , formatter:function(value,options,row){
                return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}}, 						
			{ label: '学生部门', name: 'deptName', index: 'deptName', width: 80 }, 			
			{ label: '是否通过', name: 'examResult', index: 'examResult', width: 80 ,formatter : function(cellvalue,options,rowObj){
        	    temp='';
               	if(cellvalue =="失败"){
               		temp="<span style='color:red;'>"+"失败"+"</span>";
                  	 return temp;
               		}else {
               			temp="<span style='color:green;'>"+"通关"+"</span>";
                     	 return temp;
					}
               	 }
			},
			{ label: '是否通过', name: 'examResult', index: 'examResult', width: 80, hidden:true}	
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        multiboxonly: true, // 单选.
        beforeSelectRow: beforeSelectRow, // 单选控制  触发下面定义的单选方法.
        pager: "#jqGridPager",
        jsonReader : {
            root: "scoreListVo.list",
            page: "scoreListVo.pageNum",
            total: "scoreListVo.pages",
            records: "scoreListVo.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getDept();

    // 单选控制.
    function beforeSelectRow() {
        $("#jqGrid").jqGrid('resetSelection');
        return(true);
    }
    
});
var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};
var ztree;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		eScore: {},
		showDetial:false,
		scoreDetils:{},//分数详情
		scoreDetails:{},
		pictureList:{},
		q:{
			examinationName: null,
			studentName: null,
			deptId:null,
			deptName:null,
			deptNo:null,
        },
	},
	methods: {
		getDept: function(){
			//加载部门树
			$.get(baseURL + "sys/dept/listByUser", function(r){
				ztree = $.fn.zTree.init($("#deptTree"), setting, r);
				var node = ztree.getNodeByParam("deptId", vm.q.deptId);
				if(node != null){
					ztree.selectNode(node);
					//修改时，不能回显.下边的解决了.
					//vm.workDaily.deptName = node.name;
					Vue.set(vm.q, 'deptName', node.name)
					Vue.set(vm.q, 'deptNo', node.deptNo)
				}
			})
		},
		deptTree: function(){
			layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择部门",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#deptLayer"),
				btn: ['确定', '取消'],
				btn1: function (index) {
					var node = ztree.getSelectedNodes();
					//选择上级部门
					vm.q.deptId = node[0].deptId;
					vm.q.deptName = node[0].name;
					vm.q.deptNo = node[0].deptNo;

					layer.close(index);
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.eScore = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.eScore.id == null ? "sys/escore/save" : "sys/escore/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.eScore),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "/sage/exam/score/delete",
                    contentType: "application/json",
				    data: JSON.stringify({id:ids[0]}),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/escore/info/"+id, function(r){
                vm.eScore = r.eScore;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'examinationName': vm.q.examinationName,"studentName":vm.q.studentName,"deptNo":vm.q.deptNo},
                page:page
            }).trigger("reloadGrid");
            vm.getDept();
		},
		detail:function(event){
			var id = getSelectedRow();
//			 debugger
            if(id == null){
                return;
            }
			 
			var rowData = $("#jqGrid").jqGrid('getRowData',id);
			
			console.log(rowData)
            
			vm.showList = false;
            vm.showDetial = true;
            
            vm.scoreDetils.deptName=rowData.deptName;//学生部门的名称
            vm.scoreDetils.examName=rowData.examName;//本次考试的名称
            vm.scoreDetils.examPassScore=rowData.examPassScore;//本次考试通关的分数
            vm.scoreDetils.examScore=rowData.examScore;//本次得分
            vm.scoreDetils.studentName=rowData.studentName;//学生名称
            vm.scoreDetils.subExamTime=rowData.subExamTime;//学生考试交卷时间
//            vm.scoreDetils.examResult=rowData.examResult;//学生考试交卷时间
            
            if(rowData.examResult=="失败"){
            	vm.scoreDetils.examResult="失败";
            }else{
            	vm.scoreDetils.examResult="成功";
            }
            
            var ss={
            		"examtionId":rowData.examtionId,"id":rowData.scoreId,"studentId":rowData.studentId
            }
            $.ajax({
                type: "POST",
                url: baseURL+"sage/exam/score/getScoreDetailsByUserId",
                contentType: "application/json",
			    data: JSON.stringify(ss),
			    dataType: "json",
                success: function(data){
                	console.log(data)
                	vm.scoreDetails = data.scoreDetails;
                	vm.pictureList = data.pictureList;
                	}
                });
		},
		//返回列表
		backList:function(){
			vm.showList = true;
            vm.showDetial = false;
            vm.reload();
		},
		//清空查询输入内容
		clear:function(){
			vm.q.examinationName = null
			vm.q.studentName = null
			vm.q.deptNo=null
			vm.q.deptName=null
			vm.q.deptId=null
			
			vm.reload();
		},
		imp:function(){
			if(vm.q.deptNo==null){
				vm.q.deptNo=''
			}
			if(vm.q.examinationName==null){
				vm.q.examinationName=''
			}
			if(vm.q.studentName==null){
				vm.q.studentName=''
			}
			var url = "/sage/exam/score/imp?deptNo="+vm.q.deptNo
				+"&examinationName="+vm.q.examinationName
				+"&studentName="+vm.q.studentName;
			window.location.href = baseURL +url;

		},
	}
});

//时间格式化.
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}